from flask import g, Flask
from flask_migrate import Migrate
from exts import db, mail
from models import User
from flask_login import current_user, LoginManager
import config


from blueprints.user import bp as user_bp
from blueprints.api.user import bp as api_user_bp


app = Flask(__name__)

# 设置session过期时间
# app.permanent_session_lifetime = datetime.timedelta(seconds=10*60)
# session.permanent = True

# app.app_context().push()
app.config.from_object(config)
db.init_app(app)
mail.init_app(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "visitor.login"
login_manager.login_message_category = "warning"

app.register_blueprint(user_bp)
app.register_blueprint(api_user_bp)

migrate = Migrate(app, db)


# @app.route('/favicon.ico')  # 设置icon
# def favicon():
#     return send_from_directory(
#         os.path.join(app.root_path, 'static'),  # 对于当前文件所在路径,比如这里是static下的favicon.ico
#         'favicon.ico', mimetype='image/vnd.microsoft.icon'
#     )


@login_manager.user_loader
def load_user(user_id):
    user = User.query.get(int(user_id))
    return user


# todo before_request
@app.before_request
def before_request():
    g.ctx = app.app_context()
    pass


if __name__ == '__main__':
    app.run()
